package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.TownshipModel;
import com.app.service.AdminUserService;
import com.app.service.TownService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/admin")
public class AdminTownController {
    @Autowired
    private TownService townService;
    @Autowired
    private AdminUserService adminUserService;

    //新增和编辑
    @PostMapping("/insertAndUpdateTown")
    public Result<Object> insertAndUpdateTown(@RequestBody TownshipModel townshipModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (townshipModel.getId() > 0) {
            townshipModel.setUpdateTime(CommonHelp.getNowTimestamp());
            townService.update(townshipModel);
            return Result.success("更新成功");
        } else {
            townshipModel.setCreateTime(CommonHelp.getNowTimestamp());
            townService.insert(townshipModel);
            return Result.success("新增成功");
        }
    }

    //删除
    @GetMapping("/deleteTown")
    public Result<Object> deleteTown(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        townService.delete(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getTownList")
    public Result<Object> getTownList(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(townService.getTownList());
    }

    //更新状态
    @GetMapping("/updateTownStatus")
    public Result<Object> updateTownStatus(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        townService.updateTownStatus(id, status);
        return Result.success("更新成功");
    }
}
